package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.system.bean.Role;
import com.tjtt.tdm.system.bean.UserRole;
import com.tjtt.tdm.system.dao.RoleDao;
import com.tjtt.tdm.system.service.LogService;
import com.tjtt.tdm.system.service.RoleService;
import com.tjtt.tdm.system.service.UserRoleService;

public class UserRoleAction extends BaseAction {
	private Role role;
	public RoleDao roleDao;
	public UserRole userRole;
	public RoleService roleService;
	public UserRoleService userRoleService;
	private LogService logService;
	private String id;
	private String userid;
	private String roleid;
	private String idd;
	private String roleId;
	
	public void addUserRole(){
		try{
			String toids = getRequest().getParameter("toids");
			String roleid = getRequest().getParameter("roleid");
			if (!toids.equals("")&&!roleid.equals("")){
				String[] toidarray = toids.split(",");
				for(int i=0;i<toidarray.length;i++){
					String strSql = "select * from sys_user_role t where t.userid='"+toidarray[i]+"' and t.roleid='"+roleid+"' ";
					List lists = roleService.queryListBySql(strSql);
					if (lists.size()==0){
						strSql = "insert into sys_user_role(ID,USERID,ROLEID) values (seq_user_role.nextval,'"+toidarray[i]+"','"+roleid+"')";
						roleService.exeSql(strSql);
					}
				}
			}
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void delUserRole(){
		try{
			String toids = getRequest().getParameter("toids");
			String roleid = getRequest().getParameter("roleid");
			if (!toids.equals("")&&!roleid.equals("")){
				String[] toidarray = toids.split(",");
				for(int i=0;i<toidarray.length;i++){
					String strSql = "delete from sys_user_role t where t.userid='"+toidarray[i]+"' and t.roleid='"+roleid+"' ";
					roleService.exeSql(strSql);
				}
			}
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
	}
	
	public void saveUserOfRole() throws Exception{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		//Role role = new Role();
		 String roleId = getRequest().getParameter("roleId");
		List<UserRole> userRoleList = new ArrayList<UserRole>();
		try{
			 String id = getRequest().getParameter("userid");
			 if(""!=id){
				 String[] userid = id.split(",");
				 	for(int i=0; i<userid.length; i++){
				 		UserRole ur = new UserRole();
				 		ur.setRoleid(Long.parseLong(roleId));
				 			ur.setUserid(Long.parseLong(userid[i]));
				 		userRoleList.add(ur);
				 	}
				 	userRoleService.updateURAll(roleId, userRoleList);
				}else{
					userRoleService.delUserRoleByRoleId(Long.parseLong(roleId));
				}
			/* String[] userid = id.split(",");
			 	for(int i=0; i<userid.length; i++){
			 		UserRole ur = new UserRole();
			 		ur.setRoleid(Long.parseLong(roleId));
			 		ur.setUserid(Long.parseLong(userid[i]));
			 		userRoleList.add(ur);
			 	}
			 	userRoleService.updateURAll(roleId, userRoleList);*/
			 out.print("{\"ajaxResult\":\"success\"}");
			 logService.saveLogInfo(this.getLogBase(), "系统管理", "角色管理", "角色分配人员成功！",3);
		}catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
			logService.saveLogInfo(this.getLogBase(), "系统管理", "角色管理", "角色分配人员失败！",3);
		}
	}
		

	public RoleDao getRoleDao() {
		return roleDao;
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public UserRole getUserRole() {
		return userRole;
	}
	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	public UserRoleService getUserRoleService() {
		return userRoleService;
	}

	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}

	public String getIdd() {
		return idd;
	}

	public void setIdd(String idd) {
		this.idd = idd;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

}
